﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using XbsWebAPI.Application.CampRoom.Dtos;
using XbsWebAPI.Application.CampRoom.HouseType;
using XbsWebAPI.Application.CampRoom.Room.Dtos;
using XbsWebAPI.Application.QueryModel;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.Enums;
using static XbsWebAPI.Application.EnumToDictionaryManager;

namespace XbsWebAPI.MainHost.Controllers
{
    /// <summary>
    /// 房型管理
    /// </summary>
    [ApiController]
    [Route("api/xbs_v1/[controller]")]
    public class HouseTypeController : Controller
    {
        private readonly IHouseTypeAppService _houseTypeAppService;

        public HouseTypeController(IHouseTypeAppService houseTypeAppService)
        {
            _houseTypeAppService = houseTypeAppService;
        }

        ///// <summary>
        ///// 新建房型
        ///// </summary>
        ///// <param name="model">房型输入对象</param>
        ///// <returns></returns>
        //[HttpPost,Route("[action]")]
        //public async Task<HouseType> AddHouseType(HouseType model)
        //{
        //    var result = await _houseTypeAppService.CreateHouseTypeAsync(model);
        //    return result;
        //}

        ///// <summary>
        ///// 根据Id查询房型
        ///// </summary>
        ///// <param name="id">房型Id</param>
        ///// <returns></returns>
        //[HttpGet, Route("[action]")]
        //public ResultDto<HouseType> FindHouseType(Guid id)
        //{
        //    var result = _houseTypeAppService.GetHouseTypeById(id);

        //    return result;
        //}

        /// <summary>
        /// 获得床位类型
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<Dictionary<int, string>> FindBedTypes()
        {
            var bedtypes = EnumToDictionary<BedType>();
            var result = new ResultDto<Dictionary<int, string>>();
            result.Code = 0;
            result.Message = "";
            result.Result = bedtypes;

            return result;
        }

        /// <summary>
        /// 获得房型状态
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<Dictionary<int, string>> FindHouseTypes()
        {
            var houseTypeStatuses = EnumToDictionary<HouseTypeStatus>();
            var result = new ResultDto<Dictionary<int, string>>();
            result.Code = 0;
            result.Message = "";
            result.Result = houseTypeStatuses;
            return result;
        }


        /// <summary>
        /// 分页查询房型
        /// </summary>
        /// <param name="houseType">房型状态，0-待上架，1-已上架，2-已下架</param>
        /// <param name="campId">营位Id</param>
        /// <param name="name">房型名称</param>
        /// <param name="pageNumber">第几页，默认1</param>
        /// <param name="pageSize">每页多少条，默认10</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<PageListDto<ListHouseTypeDto>> FindHouseTypePagedList(HouseTypeStatus? houseType, Guid? campId,string name, int? pageNumber, int? pageSize)
        {
            QueryHouseType query = new QueryHouseType
            {
                HouseTypeStatus = houseType,
                Name = name,
                CampId = campId,
                PageNumber = pageNumber,
                PageSize = pageSize
            };
            var result = _houseTypeAppService.GetHouseTypePagedList(query);

            return result;
        }

        /// <summary>
        /// 根据Id获取房型基本信息（第一页）
        /// </summary>
        /// <param name="id">房型Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<GetHouseTypeBasicDto> FindHouseTypeBasicById(Guid id)
        {
            var result =  _houseTypeAppService.GetHouseTypeBasicById(id);

            return result;
        }

        /// <summary>
        /// 新建房型基本信息（第一页）
        /// </summary>
        /// <param name="model"></param>
        /// <returns>返回新建房型的Id，如果用户点击“下一步”需要将Id传给下一页</returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<Guid>> AddHouseTypeBasic(CreateHouseTypeBasicDto model)
        {
            var result = await _houseTypeAppService.CreateHouseTypeBasicAsync(model);
            return result;
        }

        /// <summary>
        /// 编辑房型基本信息（第一页）
        /// </summary>
        /// <param name="model"></param>
        /// <returns>返回新建房型的Id，如果用户点击“下一步”需要将Id传给下一页</returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<Guid>> EditHouseTypeBasic(EditHouseTypeBasicDto model)
        {
            var result = await _houseTypeAppService.UpdateHouseTypeBasicAsync(model);
            return result;
        }

        /// <summary>
        /// 根据Id获取房型价格信息（第二页）
        /// </summary>
        /// <param name="id">房型Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<HouseTypePriceDto> FindHouseTypePriceById(Guid id)
        {
            var result = _houseTypeAppService.GetHouseTypePriceById(id);

            return result;
        }

        /// <summary>
        /// 设置房型价格（第二页）
        /// </summary>
        /// <param name="model"></param>
        /// <returns>返回新建房型的Id，如果用户点击“下一步”需要将Id传给下一页</returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<Guid>> SetHouseTypePrice(HouseTypePriceDto model)
        {
            var result = await _houseTypeAppService.SetHouseTypePriceAsync(model);
            return result;
        }

        /// <summary>
        /// 上/下架房型
        /// </summary>
        /// <param name="id">下架房型的Id</param>
        /// <param name="status">选择操作：1-上架，2-下架</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public async Task<ResultDto<string>> TakeHouseTypeOnOff(Guid id,int status)
        {
            var result = await _houseTypeAppService.TakeHouseTypeOnOff(id,status);
            return result;
        }


        /// <summary>
        /// 批量新建房间并绑定到当前房型（设置房型第三页）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> AddBindRooms(CreateBindRoomDto model)
        {
            var result = await _houseTypeAppService.AddBindRooms(model);
            return result;
        }

        /// <summary>
        /// 查询当前房型下的房间
        /// </summary>
        /// <param name="id">房型Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<List<GetRoomDto>> FindRoomsByHouseTypeId(Guid id)
        {
            var result = _houseTypeAppService.FindRoomsByHouseTypeId(id);
            return result;
        }

        /// <summary>
        /// 检测新增的房间是否与当前房型下的房间重名
        /// </summary>
        /// <param name="name">房间名</param>
        /// /// <param name="houseTypeId">房型Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<string> CheckRoomExisted(string name,Guid houseTypeId)
        {
            var result = _houseTypeAppService.CheckRoomExisted(name,houseTypeId);
            return result;
        }


        /// <summary>
        /// 查询指定时间的价格
        /// </summary>
        /// <param name="selectDate">选择日期</param>
        /// <param name="houseTypeId">房间类型</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<decimal> FindHouseTypeIdPriceFromDate(DateTime selectDate,Guid houseTypeId)
        {
            var result = _houseTypeAppService.FindHouseTypeIdPriceFromDate(selectDate, houseTypeId);
            return result;
        }

        /// <summary>
        /// 根据房型ID删除指定的房型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public async Task<ResultDto<string>> DeleteHouseTypeById(Guid id)
        {
            var result = await _houseTypeAppService.RemoveHouseTypeById(id);
            return result;
        }
    }
}
